package cn.sun.pet.service.impl;

import cn.sun.basic.service.impl.BaseServiceImpl;
import cn.sun.basic.util.DistanceUtil;
import cn.sun.basic.util.PageBean;
import cn.sun.basic.util.Point;
import cn.sun.basic.util.SmsUtil;
import cn.sun.org.domain.Employee;
import cn.sun.org.domain.Shop;
import cn.sun.org.mapper.EmployeeMapper;
import cn.sun.org.mapper.ShopMapper;
import cn.sun.pet.domain.Pet;
import cn.sun.pet.domain.SearchMasterMsg;
import cn.sun.pet.mapper.SearchMasterMsgMapper;
import cn.sun.pet.query.SearchMasterMsgQuery;
import cn.sun.pet.service.IPetService;
import cn.sun.pet.service.ISearchMasterMsgService;
import cn.sun.user.domain.User;
import cn.sun.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg,SearchMasterMsgQuery> implements ISearchMasterMsgService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Long loginId) {

        //1消息要分配给最近店铺
        //1.1 获取最近店铺
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.findAll());

        //1.2 把店铺绑定到消息
        searchMasterMsg.setShop(nearestShop);
        searchMasterMsg.setShop_id(nearestShop.getId());
        //1.3 用户绑定
        User user = userMapper.findByLoginInfoId(loginId);
        searchMasterMsg.setUser(user);
        searchMasterMsg.setUser_id(user.getId());
        //1.4 对象消息进行持久化
        searchMasterMsgMapper.save(searchMasterMsg);
        //2 发一个对象消息门店管理员
        Long adminId = nearestShop.getAdmin().getId();
        Employee admin = employeeMapper.findById(adminId);
        //SmsUtil.sendMsg(admin.getPhone(),"老板，来["+searchMasterMsg.getTitle()+"]生意了！赶快去接客！");
        System.out.println(admin.getPhone()+"老板，来["+searchMasterMsg.getTitle()+"]生意了！赶快去接客！");

    }

    @Override
    public PageBean<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long loginInfoId) {
        query.setState(0);
        //判断是否是店铺管管理员或员工-通过Logininfo_id查询Employee，如果有shopId就是店铺管理员或员工
        Employee employee = employeeMapper.findByLoginInfoId(loginInfoId);
        if (employee.getShopId()!=null){
            query.setShopId(employee.getShopId());
        }
        //否则就是平台管理员
        return super.findPage(query);
    }

    @Override
    public PageBean<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long loginInfoId) {
        query.setState(1);
        //判断是否是店铺管管理员或员工-通过Logininfo_id查询Employee，如果有shopId就是店铺管理员或员工
        Employee employee = employeeMapper.findByLoginInfoId(loginInfoId);
        if (employee.getShopId()!=null){
            query.setShopId(employee.getShopId());
        }
        //否则就是平台管理员
        return super.findPage(query);
    }

    @Override
    public PageBean<SearchMasterMsg> query(SearchMasterMsgQuery query, Long loginInfoId) {
        User user = userMapper.findByLoginInfoId(loginInfoId);
        query.setUserId(user.getId());
        return super.findPage(query);
    }


    @Autowired
    private IPetService petService;
    @Override
    public void handle(Pet pet) {
        //1 先根据宠物信息，查询寻主信息的ID
        Long searchMasterMsgId = pet.getSearch_master_msg_id();
        //通过id修改状态为已处理
        searchMasterMsgMapper.updateStateForHandledById(searchMasterMsgId);
        //2 创建宠物及详情 service去做
        petService.save(pet);
        //3 @TODO 创建订单

    }
//    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt) {
//        PetAcquisitionOrder order = new PetAcquisitionOrder();
//        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
//        order.setState(0);//待支付
//        order.setPrice(pet.getCostprice());
//        order.setAddress(adopt.getAddress());
//        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
//        order.setOrderSn(orderSn);
//        order.setPet_id(pet.getId());
//        order.setUser_id(adopt.getUser_id());
//        order.setPaytype(0);
//        order.setShop_id(pet.getShop_id());
//        order.setEmp_id(EmployeeContext.getLoginEployee().getId());
//        return order;
//    }
}
